/**
 * Copyright 2011 ArcBees Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package vn.nichecafe.client.ui;

import static com.google.gwt.query.client.GQuery.$;
import vn.nichecafe.client.NameTokens;
import vn.nichecafe.client.PlaceRequestChangedEvent;
import vn.nichecafe.client.ui.slider.Gallery;
import vn.nichecafe.client.ui.slider.Hot;
import vn.nichecafe.client.ui.slider.LiveMusic;
import vn.nichecafe.client.ui.slider.NicheMenu;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;

/**
 * A {@link BaseContent} styled to contain {@link SliderMenu}.
 * <p />
 * Look at {@link Menu} to see how we can use this widget within a UiBinder file
 * even though its constructor relies on dependency injection.
 * 
 * @author Christian Goudreau
 * @author Philippe Beaudoin
 */
public class Slider extends Composite {

	private final EventBus eventBus;

	public interface Binder extends UiBinder<Widget, Slider> {
	}

	@Inject
	public Slider(Binder binder, EventBus eventBus) {
		this.eventBus = eventBus;
		initWidget(binder.createAndBindUi(this));
		initButton();
	}

	private void initButton() {
		String liveHTML = "<span class=\"icon live-music\">&nbsp;</span>"
				+ "<span class=\"big-font\">LIVE MUSIC</span>"
				+ "<span class=\"small-font\">Âm nhạc cuối tuần</span>";

		String menuHTML = "<span class=\"icon menu\">&nbsp;</span>"
				+ "<span class=\"big-font\">MENU</span>"
				+ "<span class=\"small-font\">Món ngon tại NICHE</span>";
		String bookingHTML = "<span class=\"icon booking\">&nbsp;</span>"
				+ "<span class=\"big-font\">HOT</span>"
				+ "<span class=\"small-font\">Phòng họp nhóm - hội thảo</span>";
		String galleryHTML = "<span class=\"icon gallery\">&nbsp;</span>"
				+ "<span class=\"big-font\">GALLERY</span>"
				+ "<span class=\"small-font\">Thư viện hình ảnh</span>";

		acrLiveMusic.setHTML(liveHTML);
		acrMenu.setHTML(menuHTML);
		acrHot.setHTML(bookingHTML);
		acrGallery.setHTML(galleryHTML);

		changeSliderContent(new Hot(), false);
		acrCurrent = acrHot; 
		setCurrentStyle();
	}

	private boolean active;

	public void setActivateMode(boolean active) {
		if (this.active != active) {
			this.active = active;
		}
		applyMode();
	}
	

	private void applyMode() {
		sliderContent.setVisible(active);
	}

	private Anchor acrCurrent;


	@UiHandler({ "acrMenu", "acrGallery", "acrHot", "acrLiveMusic" })
	public void onClickTab(ClickEvent clickEvent) {
		Anchor acrClicked = (Anchor) clickEvent.getSource();
		changeAcr(acrClicked);

	}

	private void changeAcr(Anchor acrClicked) {
		if (!acrClicked.equals(acrCurrent)) {
			acrCurrent = acrClicked;
			setCurrentStyle();
			if (acrClicked.equals(acrGallery)) {
				changeSliderContent(new Gallery(), true);
			} else if (acrClicked.equals(acrHot)) {
				changeSliderContent(new Hot(), true);
			} else if (acrClicked.equals(acrLiveMusic)) {
				changeSliderContent(new LiveMusic(), true);
			} else {
				changeSliderContent(new NicheMenu(), true);
			}
		}
		forwardToWelcomePage();
	}

	private void setCurrentStyle() {
		acrGallery.removeStyleName("current");
		acrHot.removeStyleName("current");
		acrLiveMusic.removeStyleName("current");
		acrMenu.removeStyleName("current");
		acrCurrent.addStyleName("current");
	}

	private void changeSliderContent(Widget w, boolean animation) {
		sliderContent.clear();
		sliderContent.add(w);
		if(animation){
			w.setVisible(false);
			$(w.getElement()).fadeIn(1000);
		}
	}

	private void forwardToWelcomePage() {
		PlaceRequest pR = new PlaceRequest(NameTokens.getWelcomePage());
		PlaceRequestChangedEvent.fire(this, pR);
	}

	@UiField
	Anchor acrMenu;

	@UiField
	Anchor acrGallery;

	@UiField
	Anchor acrHot;

	@UiField
	Anchor acrLiveMusic;

	@UiField
	FlowPanel tabPanel;

	@UiField
	FlowPanel sliderContent;

	@Override
	public void fireEvent(GwtEvent<?> event) {
		eventBus.fireEvent(event);
	}
}
